import Vue from 'vue'
import Vuex from 'vuex'

//Vue使用vuex来管理全局状态
Vue.use(Vuex);

//分开写，便于单元测试
var mutations = {
  initRouter(state, routes) {//将后台加载到的route加载到store中保存，之后继续使用
    state.routes = routes;
  },
  login(state, loginInfo) {
    state.user = loginInfo.user;
    window.localStorage.setItem('user', JSON.stringify(loginInfo.user));
  },
  logout(state) {
    window.localStorage.removeItem('user');
    state.routes = [];
    //周烨 2018-7-3
    state.user = {
      name: window.localStorage.getItem('user' || '[]') == null ? '未登录' : JSON.parse(window.localStorage.getItem('user' || '[]')).name,
      userface: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).userface,
      username: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).username,
      roles: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).roles,
      role: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).role,//当前选中的角色 胡杨升 2018年10月8日
      sslx:window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).sslx,//胡杨升 2018年10月8日
      sslxbh:window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).sslxbh//胡杨升 2018年10月8日
    };
  },
  switchrole(state){// 切换角色-清空菜单
    state.routes = [];
  },
  toggleNFDot(state, newValue) {
    state.nfDot = newValue;
  },
  updateMsgList(state, newMsgList) {
    state.msgList = newMsgList;
  },
  updateCurrentFriend(state, newFriend) {
    state.currentFriend = newFriend;
  },
  addValue2DotMap(state, key) {
    state.isDotMap.set(key, "您有未读消息")
  },
  removeValueDotMap(state, key) {
    state.isDotMap.delete(key);
  },
  selectRole(state, role){
    state.user.role = role;
  }
};
var actions = {
  // connect(context){
  //   context.state.stomp = Stomp.over(new SockJS("/ws/endpointChat"));
  //   context.state.stomp.connect({}, frame=> {
  //     context.state.stomp.subscribe("/user/queue/chat", message=> {
  //       var msg = JSON.parse(message.body);
  //       var oldMsg = window.localStorage.getItem(context.state.user.username + "#" + msg.from);
  //       if (oldMsg == null) {
  //         oldMsg = [];
  //         oldMsg.push(msg);
  //         window.localStorage.setItem(context.state.user.username + "#" + msg.from, JSON.stringify(oldMsg))
  //       } else {
  //         var oldMsgJson = JSON.parse(oldMsg);
  //         oldMsgJson.push(msg);
  //         window.localStorage.setItem(context.state.user.username + "#" + msg.from, JSON.stringify(oldMsgJson))
  //       }
  //       if (msg.from != context.state.currentFriend.username) {
  //         context.commit("addValue2DotMap", "isDot#" + context.state.user.username + "#" + msg.from);
  //       }
  //       //更新msgList
  //       var oldMsg2 = window.localStorage.getItem(context.state.user.username + "#" + context.state.currentFriend.username);
  //       if (oldMsg2 == null) {
  //         context.commit('updateMsgList', []);
  //       } else {
  //         context.commit('updateMsgList', JSON.parse(oldMsg2));
  //       }
  //     });
  //     context.state.stomp.subscribe("/topic/nf", message=> {
  //       context.commit('toggleNFDot', true);
  //     });
  //   }, failedMsg=> {
  //
  //   });
  // }
};

//实例一个store用来管理状态
var indexStore = new Vuex.Store({
  state: {
    user: {
      name: window.localStorage.getItem('user' || '[]') == null ? '未登录' : JSON.parse(window.localStorage.getItem('user' || '[]')).name,
      userface: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).userface,
      username: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).username,
      roles: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).roles,
      role: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).role,//当前选中的角色 胡杨升 2018年10月8日
      sslx:window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).sslx,//胡杨升 2018年10月8日
      sslxbh:window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).sslxbh//胡杨升 2018年10月8日
    },
    routes: [],
    msgList: [],
    isDotMap: new Map(),
    currentFriend: {},
    nfDot: false
  }, // 一整个状态树
  mutations: mutations // 同步执行，改变状态
  // actions: actions// 异步执行，改变状态
});

//导出
export default indexStore;
